/*#127 Start Luvina Modify*/
$(function() {
    _XHR = {
            onReady : true,
            targetAppend : undefined,
            targetSelf : undefined,
            callbackFunc: undefined,
            ajaxStyle : "",
            successdefault: function(respones) {
                if(respones.content) {
                    if(_XHR.ajaxStyle == "clear") {
                        _XHR.targetAppend.empty();
                    }
                    _XHR.targetAppend.append(respones.content);
                }
                if(respones.status_code == 2) {
                    _XHR.targetSelf.remove();
                }
                if(respones.page) {
                    _XHR.changeParam('page', respones.page);
                }
            },
            success : function (respones) {
                _XHR.successdefault(respones);
            },
            changeParam: function(param_name, param_value){
                try {
                    var params = JSON.parse(this.targetSelf.attr('params'));
                    params[param_name] = param_value;
                    this.targetSelf.attr('params', JSON.stringify(params));
                } catch (e) {
                }
            },
            error : function (ex) {
                console.log(ex.statusText);
            },
            formSerialize : function (frmid) {
                var frmparams = $("#" + frmid).serializeArray();
                return frmparams;
            },
            prepare : function(){
                if(_XHR.onReady == false) {
                    return false;
                }
                _XHR.onReady = false; /*do not acceept another requrest*/
                _XHR.targetSelf.unbind('click');
                $.fancybox.showLoading();
            },
            finish : function(){
                _XHR.onReady = true;
                _XHR.targetSelf.bind('click', _XHR.callbackFunc);
                $.fancybox.hideLoading();
                _XHR.success = _XHR.successdefault;
            },
            open : function(uri, params, action_name) {
                if(this.prepare() == false) {
                    return;
                }
                if(!params[action_name]) {
                    params[action_name] = true;
                }
                $.ajax({
                    type    : "POST",
                    url     : '/' + uri + '/?' + action_name + '=true',
                    cache   : false,
                    data    : params,
                    success : function(respones) {
                        _XHR.success(respones);
                    },
                    error   : function(ex) {
                        _XHR.error(ex);
                    }
                }).done(function() {
                    _XHR.finish();
                });
            //#133 Start Luvina Modify
            },
            openAsXMLHttpRequest : function(uri, params, action_name) {
            	if(this.prepare() == false) {
                    return;
                }
            	var myxhr = new XMLHttpRequest();
            	myxhr.open("POST", '/' + uri + '/?' + action_name + '=true', true);
            	myxhr.responseType = "text/plain";
            	myxhr.send(params);
            	myxhr.onreadystatechange = function () {
                    if (myxhr.readyState == 4 && myxhr.status == 200) {
                    	_XHR.success(myxhr.responseText);
                    }
                    if(myxhr.readyState == 4) {
                    	_XHR.finish();
                    }
                };
            }
            //#133 End Luvina Modify
        };
    uibindajaxload = function(target){
        var xhr = _XHR;
        if(!_XHR.onReady) { 
            return false;
        }
        xhr.targetAppend = $("#" + $(target).attr('target-append'));
        xhr.targetSelf = $(target);
        xhr.callbackFunc = function() {
            uibindajaxload(target);
        };
        var action_name = $(target).attr('action');
        var ajaxStyle = $(target).attr('ajaxStyle');
        xhr.ajaxStyle = ajaxStyle;
        var uri = $(target).attr('uri');
        var params = {};
        try {
            params = $.parseJSON($(target).attr('params'));
        } catch(e) {
            console.log('ERROR: parse json fail!');
        }
        xhr.open(uri, params, action_name);
    };
    
    $('.uibindajaxload').click(function() {
        uibindajaxload(this);
    });
    
    $('.uibindajaxload').each(function() {
        var target = this;
        var params = {};
        try {
            params = $.parseJSON($(target).attr('params'));
        } catch(e) {
        }
        if($(target).attr('user-data-form')) {
            var frmparams = _XHR.formSerialize($(target).attr('user-data-form'));
            $.each(frmparams, function(i, el) {
                params[el.name] = el.value;
            });
            $(this).attr('params', JSON.stringify(params));
        }
    });
    uiBindAjaxLoadFancybox = function(target) {
        var xhr = _XHR;
        if(!_XHR.onReady) { 
            return false;
        }
        xhr.targetSelf = $(target);
        xhr.callbackFunc = function() {
            uiBindAjaxLoadFancybox(target);
        };
        var prepare = xhr.prepare;
        xhr.prepare = function() {
            prepare();
            _XHR.targetSelf.bind('click',  function() {return false;});
        };
        xhr.success = function(response) {
            /*#131 Start Luvina Modify*/
        	try {
	        	$.fancybox({
	                content : response,
	                type    : 'ajax',
	                wrapCSS : 'fancybox-cocoyaku',
	                padding : 15,
	                margin  : 10
	            });
            } catch (e) {
            	
            }
            /*#131 End Luvina Modify*/
        };
        var action_name = $(target).attr('action');
        var uri = $(target).attr('uri');
        var params = {};
        var frmid = $(target).attr('user-data-form');
        //#133 Start Luvina Modify
        if($('#' + frmid).attr('enctype') == "multipart/form-data") {
            var formElement = document.getElementById(frmid);
            var fd = new FormData(formElement);
            xhr.openAsXMLHttpRequest(uri, fd, action_name);
        } else {
            var frmparams = xhr.formSerialize(frmid);
            $.each(frmparams, function(i, el) {
                params[el.name] = el.value;
            });
            xhr.open(uri, params, action_name);
        }
      //#133 End Luvina Modify
        return false;
    };
    $('.uibindajaxloadfancybox').click(function() {
        return uiBindAjaxLoadFancybox(this);
    });
    /*#132 Start Luvina Modify*/
    $('body').on('click', 'a._fancybox', function() {
        return uiBindAjaxLoadFancybox(this);
    });
    /*#132 End Luvina Modify*/
    /*#127 Start Luvina Modify*/
    $('body').on('submit', '.popup form', function() {
        $(this).find('.default').click();
        return false;
    });
    /*#127 End Luvina Modify*/
});
/*#127 End Luvina Modify*/
